package UI;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import thesis.AlgorithmParameters;
import thesis.LocationBounds;



public class ParameterSelectionDialog extends JDialog implements ActionListener{
	private static final long serialVersionUID = 1L;
	
	private AlgorithmParameters algParameters = new AlgorithmParameters();
	
	private JButton ok;
	
	private JComboBox<Integer> bd_month_field;
	private JComboBox<Integer> bd_day_field;
	private JComboBox<Integer> bd_year_field;
	private JComboBox<Integer> bd_hour_field;
	private JComboBox<Integer> bd_minute_field;
	
	private JCheckBox limitation_field;
	private JComboBox<Integer> number_tweets_field;
	private JComboBox<Integer> ed_month_field;
	private JComboBox<Integer> ed_day_field;
	private JComboBox<Integer> ed_year_field;
	private JComboBox<Integer> ed_hour_field;
	private JComboBox<Integer> ed_minute_field;
	
	private JCheckBox area_interest_field;
	private JComboBox<Double> bl_lat_field;
	private JComboBox<Double> bl_lon_field;
	private JComboBox<Double> ur_lat_field;
	private JComboBox<Double> ur_lon_field;
	
	private JComboBox<Integer> number_of_pages_field;
	private JComboBox<Integer> summary_dim_field;
	private JComboBox<Double> quality_weight_field;
	private JComboBox<Double> coverage_weight_field;
	private JComboBox<Double> diversity_weight_field;
	
	public ParameterSelectionDialog(){
		setBounds(0, 0, GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width, GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		
		Container c = this.getContentPane();
		c.setLayout(new GridLayout(11, 1));
		
		JPanel panelTitle = new JPanel();
		panelTitle.setLayout(new GridLayout(1,1));
		JLabel title = new JLabel("PARAMETER SELECTION");
		Font fontTitle = new Font("Arial", Font.BOLD, 40);
		title.setFont(fontTitle);
		title.setHorizontalAlignment(JLabel.CENTER);
		panelTitle.add(title);
		c.add(panelTitle);
		
		JPanel panelBeginDateLabel = new JPanel();
		panelBeginDateLabel.setLayout(new GridLayout(1,1));
		JLabel beginDateLabel = new JLabel("Begin Date");
		Font fontSubtitle = new Font("Arial", Font.BOLD, 25);
		beginDateLabel.setFont(fontSubtitle);
		beginDateLabel.setHorizontalAlignment(JLabel.CENTER);
		panelBeginDateLabel.add(beginDateLabel);
		panelBeginDateLabel.setBackground(Color.white);
		c.add(panelBeginDateLabel);
		
		JPanel panelBeginDate = new JPanel();
		panelBeginDate.setLayout(new GridLayout(1,5));
		JPanel panelBeginDateMonth = new JPanel();
		JLabel beginDateMonthLabel = new JLabel("Month: ");
		Font fontText = new Font("Arial", Font.PLAIN, 22);
		beginDateMonthLabel.setFont(fontText);
		beginDateMonthLabel.setHorizontalAlignment(JLabel.CENTER);
		panelBeginDateMonth.add(beginDateMonthLabel);
		bd_month_field = new JComboBox<Integer>();
		for (int i=1; i<=12; i++){
			bd_month_field.addItem(i);
		}
		bd_month_field.setSelectedItem(3);
		panelBeginDateMonth.add(bd_month_field);
		panelBeginDateMonth.setBackground(Color.white);
		panelBeginDate.add(panelBeginDateMonth);
		JPanel panelBeginDateDay = new JPanel();
		JLabel beginDateDayLabel = new JLabel("Day: ");
		beginDateDayLabel.setFont(fontText);
		beginDateDayLabel.setHorizontalAlignment(JLabel.CENTER);
		panelBeginDateDay.add(beginDateDayLabel);
		bd_day_field = new JComboBox<Integer>();
		for (int i=1; i<=31; i++){
			bd_day_field.addItem(i);
		}
		bd_day_field.setSelectedItem(2);
		panelBeginDateDay.add(bd_day_field);
		panelBeginDateDay.setBackground(Color.white);
		panelBeginDate.add(panelBeginDateDay);
		JPanel panelBeginDateYear = new JPanel();
		JLabel beginDateYearLabel = new JLabel("Year: ");
		beginDateYearLabel.setFont(fontText);
		beginDateYearLabel.setHorizontalAlignment(JLabel.CENTER);
		panelBeginDateYear.add(beginDateYearLabel);
		bd_year_field = new JComboBox<Integer>();
		bd_year_field.addItem(2012);
		bd_year_field.addItem(2013);
		bd_year_field.setSelectedItem(2013);
		panelBeginDateYear.add(bd_year_field);
		panelBeginDateYear.setBackground(Color.white);
		panelBeginDate.add(panelBeginDateYear);
		JPanel panelBeginDateHour = new JPanel();
		JLabel beginDateHourLabel = new JLabel("Hour: ");
		beginDateHourLabel.setFont(fontText);
		beginDateHourLabel.setHorizontalAlignment(JLabel.CENTER);
		panelBeginDateHour.add(beginDateHourLabel);
		bd_hour_field = new JComboBox<Integer>();
		for (int i=0; i<=23; i++){
			bd_hour_field.addItem(i);
		}
		bd_hour_field.setSelectedItem(14);
		panelBeginDateHour.add(bd_hour_field);
		panelBeginDateHour.setBackground(Color.white);
		panelBeginDate.add(panelBeginDateHour);
		JPanel panelBeginDateMinute = new JPanel();
		JLabel beginDateMinuteLabel = new JLabel("Minute: ");
		beginDateMinuteLabel.setFont(fontText);
		beginDateMinuteLabel.setHorizontalAlignment(JLabel.CENTER);
		panelBeginDateMinute.add(beginDateMinuteLabel);
		bd_minute_field = new JComboBox<Integer>();
		for (int i=0; i<=59; i++){
			bd_minute_field.addItem(i);
		}
		bd_minute_field.setSelectedItem(10);
		panelBeginDateMinute.add(bd_minute_field);
		panelBeginDateMinute.setBackground(Color.white);
		panelBeginDate.add(panelBeginDateMinute);
		c.add(panelBeginDate);
		
		JPanel panelEndDateLabel = new JPanel();
		JLabel endDateLabel = new JLabel("Range Limit");
		endDateLabel.setFont(fontSubtitle);
		endDateLabel.setHorizontalAlignment(JLabel.CENTER);
		panelEndDateLabel.add(endDateLabel);
		panelEndDateLabel.setBackground(Color.lightGray);
		c.add(panelEndDateLabel);
		
		JPanel panelLimitation = new JPanel();
		panelLimitation.setLayout(new GridLayout(1,2));
		JPanel panelCheckLimitation = new JPanel();
		JLabel checkEndDateLabel = new JLabel("Number of Tweets / End Date? ");
		checkEndDateLabel.setFont(fontText);
		checkEndDateLabel.setHorizontalAlignment(JLabel.CENTER);
		panelCheckLimitation.add(checkEndDateLabel);
		limitation_field = new JCheckBox();
		limitation_field.setSelected(true);
		limitation_field.addActionListener(this);
		panelCheckLimitation.add(limitation_field);
		panelCheckLimitation.setBackground(Color.lightGray);
		panelLimitation.add(panelCheckLimitation);
		JPanel panelNumberTweets = new JPanel();
		JLabel numberTweetsLabel = new JLabel("Number of Tweets: ");
		numberTweetsLabel.setFont(fontText);
		numberTweetsLabel.setHorizontalAlignment(JLabel.CENTER);
		panelNumberTweets.add(numberTweetsLabel);
		number_tweets_field = new JComboBox<Integer>();
		number_tweets_field.addItem(1000);
		number_tweets_field.addItem(2000);
		number_tweets_field.addItem(5000);
		number_tweets_field.addItem(10000);
		number_tweets_field.addItem(20000);
		number_tweets_field.addItem(30000);
		number_tweets_field.addItem(40000);
		number_tweets_field.addItem(50000);
		number_tweets_field.addItem(80000);
		number_tweets_field.addItem(100000);
		number_tweets_field.setSelectedItem(10000);
		panelNumberTweets.add(number_tweets_field);
		panelNumberTweets.setBackground(Color.lightGray);
		panelLimitation.add(panelNumberTweets);
		c.add(panelLimitation);
		
		JPanel panelEndDate = new JPanel();
		panelEndDate.setLayout(new GridLayout(1,5));
		JPanel panelEndDateMonth = new JPanel();
		JLabel endDateMonthLabel = new JLabel("Month: ");
		endDateMonthLabel.setFont(fontText);
		endDateMonthLabel.setHorizontalAlignment(JLabel.CENTER);
		panelEndDateMonth.add(endDateMonthLabel);
		ed_month_field = new JComboBox<Integer>();
		for (int i=1; i<=12; i++){
			ed_month_field.addItem(i);
		}
		ed_month_field.setSelectedItem(3);
		ed_month_field.setEnabled(false);
		panelEndDateMonth.add(ed_month_field);
		panelEndDateMonth.setBackground(Color.lightGray);
		panelEndDate.add(panelEndDateMonth);
		JPanel panelEndDateDay = new JPanel();
		JLabel endDateDayLabel = new JLabel("Day: ");
		endDateDayLabel.setFont(fontText);
		endDateDayLabel.setHorizontalAlignment(JLabel.CENTER);
		panelEndDateDay.add(endDateDayLabel);
		ed_day_field = new JComboBox<Integer>();
		for (int i=1; i<=31; i++){
			ed_day_field.addItem(i);
		}
		ed_day_field.setSelectedItem(2);
		ed_day_field.setEnabled(false);
		panelEndDateDay.add(ed_day_field);
		panelEndDateDay.setBackground(Color.lightGray);
		panelEndDate.add(panelEndDateDay);
		JPanel panelEndDateYear = new JPanel();
		JLabel endDateYearLabel = new JLabel("Year: ");
		endDateYearLabel.setFont(fontText);
		endDateYearLabel.setHorizontalAlignment(JLabel.CENTER);
		panelEndDateYear.add(endDateYearLabel);
		ed_year_field = new JComboBox<Integer>();
		ed_year_field.addItem(2012);
		ed_year_field.addItem(2013);
		ed_year_field.setSelectedItem(2013);
		ed_year_field.setEnabled(false);
		panelEndDateYear.add(ed_year_field);
		panelEndDateYear.setBackground(Color.lightGray);
		panelEndDate.add(panelEndDateYear);
		JPanel panelEndDateHour = new JPanel();
		JLabel endDateHourLabel = new JLabel("Hour: ");
		endDateHourLabel .setFont(fontText);
		endDateHourLabel .setHorizontalAlignment(JLabel.CENTER);
		panelEndDateHour.add(endDateHourLabel );
		ed_hour_field = new JComboBox<Integer>();
		for (int i=0; i<=23; i++){
			ed_hour_field.addItem(i);
		}
		ed_hour_field.setSelectedItem(15);
		ed_hour_field.setEnabled(false);
		panelEndDateHour.add(ed_hour_field);
		panelEndDateHour.setBackground(Color.lightGray);
		panelEndDate.add(panelEndDateHour);
		JPanel panelEndDateMinute = new JPanel();
		JLabel endDateMinuteLabel = new JLabel("Minute: ");
		endDateMinuteLabel.setFont(fontText);
		endDateMinuteLabel.setHorizontalAlignment(JLabel.CENTER);
		panelEndDateMinute.add(endDateMinuteLabel);
		ed_minute_field = new JComboBox<Integer>();
		for (int i=0; i<=59; i++){
			ed_minute_field.addItem(i);
		}
		ed_minute_field.setSelectedItem(10);
		ed_minute_field.setEnabled(false);
		panelEndDateMinute.add(ed_minute_field);
		panelEndDateMinute.setBackground(Color.lightGray);
		panelEndDate.add(panelEndDateMinute);
		c.add(panelEndDate);
		
		JPanel panelAreaInterestLabel = new JPanel();
		panelAreaInterestLabel.setLayout(new GridLayout(1,1));
		JLabel areaInterestLabel = new JLabel("Area of Interest");
		areaInterestLabel.setFont(fontSubtitle);
		areaInterestLabel.setHorizontalAlignment(JLabel.CENTER);
		panelAreaInterestLabel.add(areaInterestLabel);
		panelAreaInterestLabel.setBackground(Color.white);
		c.add(panelAreaInterestLabel);
		
		JPanel panelAreaInterest = new JPanel();
		panelAreaInterest.setLayout(new GridLayout(1,5));
		JPanel panelCheckAreaInterest = new JPanel();
		JLabel areaInterestQuestionLabel = new JLabel("Yes/No? ");
		areaInterestQuestionLabel.setFont(fontText);
		areaInterestQuestionLabel.setHorizontalAlignment(JLabel.CENTER);
		panelCheckAreaInterest.add(areaInterestQuestionLabel);
		area_interest_field = new JCheckBox();
		area_interest_field.setSelected(true);
		area_interest_field.addActionListener(this);
		panelCheckAreaInterest.add(area_interest_field);
		panelCheckAreaInterest.setBackground(Color.white);
		panelAreaInterest.add(panelCheckAreaInterest);
		JPanel panelBLLat = new JPanel();
		JLabel blLatitudeLabel = new JLabel("Bottom-Left Latitude: ");
		blLatitudeLabel.setFont(fontText);
		blLatitudeLabel.setHorizontalAlignment(JLabel.CENTER);
		panelBLLat.add(blLatitudeLabel);
		bl_lat_field = new JComboBox<Double>();
		for(double i=-90; i<=90; i++){
			bl_lat_field.addItem(i);
		}
		bl_lat_field.setSelectedItem((double)24);
		panelBLLat.add(bl_lat_field);
		panelBLLat.setBackground(Color.white);
		panelAreaInterest.add(panelBLLat);
		JPanel panelBLLon = new JPanel();
		JLabel blLongitudeLabel = new JLabel("Bottom-Left Longitude: ");
		blLongitudeLabel.setFont(fontText);
		blLongitudeLabel.setHorizontalAlignment(JLabel.CENTER);
		panelBLLon.add(blLongitudeLabel);
		bl_lon_field = new JComboBox<Double>();
		for(double i=-180; i<=180; i++){
			bl_lon_field.addItem(i);
		}
		bl_lon_field.setSelectedItem((double)-126);
		panelBLLon.add(bl_lon_field);
		panelBLLon.setBackground(Color.white);
		panelAreaInterest.add(panelBLLon);
		JPanel panelURLat = new JPanel();
		JLabel urLatitudeLabel = new JLabel("Upper-Right Latitude: ");
		urLatitudeLabel.setFont(fontText);
		urLatitudeLabel.setHorizontalAlignment(JLabel.CENTER);
		panelURLat.add(urLatitudeLabel);
		ur_lat_field = new JComboBox<Double>();
		for(double i=-90; i<=90; i++){
			ur_lat_field.addItem(i);
		}
		ur_lat_field.setSelectedItem((double)52);
		panelURLat.add(ur_lat_field);
		panelURLat.setBackground(Color.white);
		panelAreaInterest.add(panelURLat);
		JPanel panelURLon = new JPanel();
		JLabel urLongitudeLabel = new JLabel("Upper-Right Longitude: ");
		urLongitudeLabel.setFont(fontText);
		urLongitudeLabel.setHorizontalAlignment(JLabel.CENTER);
		panelURLon.add(urLongitudeLabel);
		ur_lon_field = new JComboBox<Double>();
		for(double i=-180; i<=180; i++){
			ur_lon_field.addItem(i);
		}
		ur_lon_field.setSelectedItem((double)-62);
		panelURLon.add(ur_lon_field);
		panelURLon.setBackground(Color.white);
		panelAreaInterest.add(panelURLon);
		c.add(panelAreaInterest);
		
		JPanel panelParametersLabel = new JPanel();
		panelParametersLabel.setLayout(new GridLayout(1,1));
		JLabel parametersLabel = new JLabel("Algorithm Parameters");
		parametersLabel.setFont(fontSubtitle);
		parametersLabel.setHorizontalAlignment(JLabel.CENTER);
		panelParametersLabel.add(parametersLabel);
		panelParametersLabel.setBackground(Color.lightGray);
		c.add(panelParametersLabel);
		
		JPanel panelParameters = new JPanel();
		panelParameters.setLayout(new GridLayout(1,5));
		JPanel panelSumDim = new JPanel();
		JLabel sumDimLabel = new JLabel("Summary Dimension: ");
		sumDimLabel.setFont(fontText);
		sumDimLabel.setHorizontalAlignment(JLabel.CENTER);
		panelSumDim.add(sumDimLabel);
		summary_dim_field = new JComboBox<Integer>();
		for(int i=5; i<=200; i+=5){
			summary_dim_field.addItem(i);
		}
		summary_dim_field.setSelectedItem(10);
		panelSumDim.add(summary_dim_field);
		panelSumDim.setBackground(Color.lightGray);
		panelParameters.add(panelSumDim);
		JPanel panelNumOfPages = new JPanel();
		JLabel numOfPagesLabel = new JLabel("Number of Summary Pages: ");
		numOfPagesLabel.setFont(fontText);
		numOfPagesLabel.setHorizontalAlignment(JLabel.CENTER);
		panelNumOfPages.add(numOfPagesLabel);
		number_of_pages_field = new JComboBox<Integer>();
		for(int i=1; i<=5; i++){
			number_of_pages_field.addItem(i);
		}
		number_of_pages_field.setSelectedItem(1);
		panelNumOfPages.add(number_of_pages_field);
		panelNumOfPages.setBackground(Color.lightGray);
		panelParameters.add(panelNumOfPages);
		JPanel panelQualWeight = new JPanel();
		JLabel qualWeightLabel = new JLabel("Quality Weight: ");
		qualWeightLabel.setFont(fontText);
		qualWeightLabel.setHorizontalAlignment(JLabel.CENTER);
		panelQualWeight.add(qualWeightLabel);
		quality_weight_field = new JComboBox<Double>();
		for (int i=0; i<=10; i++){
			quality_weight_field.addItem((double)i/10);
		}
		quality_weight_field.setSelectedItem((double)0.0);
		panelQualWeight.add(quality_weight_field);
		panelQualWeight.setBackground(Color.lightGray);
		panelParameters.add(panelQualWeight);
		JPanel panelCovWeight = new JPanel();
		JLabel covWeightLabel = new JLabel("Coverage Weight: ");
		covWeightLabel.setFont(fontText);
		covWeightLabel.setHorizontalAlignment(JLabel.CENTER);
		panelCovWeight.add(covWeightLabel);
		coverage_weight_field = new JComboBox<Double>();
		for (int i=0; i<=10; i++){
			coverage_weight_field.addItem((double)i/10);
		}
		coverage_weight_field.setSelectedItem((double)1.0);
		panelCovWeight.add(coverage_weight_field);
		panelCovWeight.setBackground(Color.lightGray);
		panelParameters.add(panelCovWeight);
		JPanel panelDivWeight = new JPanel();
		JLabel divWeightLabel = new JLabel("Diversity Weight: ");
		divWeightLabel.setFont(fontText);
		divWeightLabel.setHorizontalAlignment(JLabel.CENTER);
		panelDivWeight.add(divWeightLabel);
		diversity_weight_field = new JComboBox<Double>();
		for (int i=0; i<=10; i++){
			diversity_weight_field.addItem((double)i/10);
		}
		diversity_weight_field.setSelectedItem((double)0.0);
		panelDivWeight.add(diversity_weight_field);
		panelDivWeight.setBackground(Color.lightGray);
		panelParameters.add(panelDivWeight);
		c.add(panelParameters);
		
		JPanel panelButtonOk = new JPanel();
		ok = new JButton("Ok");
		ok.addActionListener(this);
		panelButtonOk.add(ok);
		
		c.add(panelButtonOk);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == limitation_field){
			if(limitation_field.isSelected()){
				number_tweets_field.setEnabled(true);
				ed_year_field.setEnabled(false);
				ed_month_field.setEnabled(false);
				ed_day_field.setEnabled(false);
				ed_hour_field.setEnabled(false);
				ed_minute_field.setEnabled(false);
			}
			else{
				number_tweets_field.setEnabled(false);
				ed_year_field.setEnabled(true);
				ed_month_field.setEnabled(true);
				ed_day_field.setEnabled(true);
				ed_hour_field.setEnabled(true);
				ed_minute_field.setEnabled(true);
			}
		}
		
		if (e.getSource() == area_interest_field){
			if (area_interest_field.isSelected()){
				bl_lat_field.setEnabled(true);
				bl_lon_field.setEnabled(true);
				ur_lat_field.setEnabled(true);
				ur_lon_field.setEnabled(true);
			}
			else {
				bl_lat_field.setEnabled(false);
				bl_lon_field.setEnabled(false);
				ur_lat_field.setEnabled(false);
				ur_lon_field.setEnabled(false);
			}
		}
		
		if(e.getSource() == ok){
			boolean errorPresence = false;
			
			Integer bdYear = (Integer)bd_year_field.getSelectedItem();
			Integer bdMonth = (Integer)bd_month_field.getSelectedItem() - 1;
			Integer bdDay = (Integer)bd_day_field.getSelectedItem();
			Integer bdHour = (Integer)bd_hour_field.getSelectedItem();
			Integer bdMinute = (Integer)bd_minute_field.getSelectedItem();
			algParameters.setBeginDate(new GregorianCalendar(bdYear, bdMonth, bdDay, bdHour, bdMinute));
			
			if(limitation_field.isSelected()){
				algParameters.setEndDate(null);
				algParameters.setLimitOfTweets((Integer)number_tweets_field.getSelectedItem());
			}
			else{
				Integer edYear = (Integer)ed_year_field.getSelectedItem();
				Integer edMonth = (Integer)ed_month_field.getSelectedItem() - 1;
				Integer edDay = (Integer)ed_day_field.getSelectedItem();
				Integer edHour = (Integer)ed_hour_field.getSelectedItem();
				Integer edMinute = (Integer)ed_minute_field.getSelectedItem();
				algParameters.setEndDate(new GregorianCalendar(edYear, edMonth, edDay, edHour, edMinute));
			
				if(algParameters.getBeginDate().getTimeInMillis() >= algParameters.getEndDate().getTimeInMillis()){
					JOptionPane.showMessageDialog(this, "Begin date must be precedent to end date", "Error", JOptionPane.ERROR_MESSAGE);
					errorPresence = true;
				}
			}
			
			if(area_interest_field.isSelected()){
				double bottomleft_lat = (Double)bl_lat_field.getSelectedItem();
				double bottomleft_lon = (Double)bl_lon_field.getSelectedItem();
				double upperright_lat = (Double)ur_lat_field.getSelectedItem();
				double upperright_lon = (Double)ur_lon_field.getSelectedItem();
				algParameters.setAreaOfInterest(new LocationBounds(bottomleft_lat, bottomleft_lon, upperright_lat, upperright_lon));
				
				if(bottomleft_lat >= upperright_lat || bottomleft_lon >= upperright_lon){
					JOptionPane.showMessageDialog(this, "Bottom-left coordinates must be ", "Error", JOptionPane.ERROR_MESSAGE);
					errorPresence = true;
				}
			}
			else{
				algParameters.setAreaOfInterest(null);
			}
			
			algParameters.setSummaryDim((Integer)summary_dim_field.getSelectedItem());
			algParameters.setNumberOfPages((Integer) number_of_pages_field.getSelectedItem());
			algParameters.setQualityWeight((Double) quality_weight_field.getSelectedItem());
			algParameters.setCoverageWeight((Double) coverage_weight_field.getSelectedItem());
			algParameters.setDiversityWeight((Double) diversity_weight_field.getSelectedItem());

			if(!errorPresence){
				this.dispose();
			}
		}
		
	}
	
	
	public AlgorithmParameters getAlgorithmParameters(){
		return algParameters;
	}
}
